package com.st.buy.cart.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author 张双虎
 */
@Configuration
public class RestConfig {
    //表示要负载均衡，其实就已经用到了ribbon
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory clientHttpRequestFactory){
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        return restTemplate;
    }
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory simpleClientHttpRequestFactory =
                new SimpleClientHttpRequestFactory();
//        simpleClientHttpRequestFactory.setReadTimeout(20000);
//        simpleClientHttpRequestFactory.setConnectTimeout(20000);
        //还可以设置其他的相关属性
        return  simpleClientHttpRequestFactory;
    }

//    @Bean
    public IRule rule(){
        return new RandomRule();
    }
}
